package org.lex.swing.widgets;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Graphics;

import javax.swing.JComponent;
import javax.swing.JSeparator;

/**
 * this kind of a decorate, can use this to "wrap" another component
 * 
 * @author Daniel Liu
 * 
 */
public class ComponentSeparator extends JComponent {
	private static final int DEFAULT_GAP = 5;
	private Component comp = null;

	public ComponentSeparator(Component comp) {
		this(comp, DEFAULT_GAP);
	}

	public ComponentSeparator(Component comp, int gap) {
		super();
		this.comp = comp;
		this.setLayout(new BorderLayout(gap, 0));
		this.add(this.comp, BorderLayout.LINE_START);
		this.add(new CenterSeparator(), BorderLayout.CENTER);
	}

	public Component getComponent() {
		return this.comp;
	}

	private static class CenterSeparator extends JSeparator {
		protected void paintComponent(Graphics g) {
			int width = this.getWidth();
			int y = this.getHeight() / 2;
			g.setColor(this.getForeground());
			g.drawLine(0, y, width, y);
			g.setColor(this.getBackground());
			g.drawLine(0, y + 1, width, y + 1);
		}
	}
}
